package com.gy.paidao.setting;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.gy.paidao.BaseActivity;
import com.gy.paidao.R;
import com.gy.paidao.bean.BaseBean;
import com.gy.paidao.util.AnimUtil;
import com.gy.paidao.util.KeyBoard;
import com.gy.paidao.util.SPHelper;
import com.gy.paidao.util.Urls;
import com.jky.struct2.http.core.AjaxParams;

/**
 * 作者：gy on 16/4/27
 * 说明：设置输入内容界面
 */
public class SettingContentActivity extends BaseActivity{

    private TextView tvCancel,tvTitle,tvFinish;
    private EditText etContent;
    private String userLoginID;
    private String strContent,strTitle;
    private int intoFlag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_content);
        setViews();
    }

    @Override
    protected void initVariable() {
        userLoginID = SPHelper.getInstance(this).getStringData("userLoginID",null);
        strTitle = getIntent().getStringExtra("title");
        intoFlag = getIntent().getIntExtra("into",0);
    }

    @Override
    protected void setTitleViews() {

    }

    @Override
    protected void setViews() {
        tvCancel = (TextView) findViewById(R.id.set_content_tv_cancel);
        tvTitle = (TextView) findViewById(R.id.set_content_tv_title);
        tvFinish = (TextView) findViewById(R.id.set_content_tv_finish);
        etContent = (EditText) findViewById(R.id.set_content_et);

        tvCancel.setOnClickListener(this);
        tvFinish.setOnClickListener(this);

        tvTitle.setText(strTitle);
        KeyBoard.showKeyBoard(this,etContent);
    }

    @Override
    protected void doClickAction(int viewId) {
        super.doClickAction(viewId);
        switch (viewId){
            case R.id.set_content_tv_cancel:
                finish();
                AnimUtil.pushRightInAndOut(this);
                break;
            case R.id.set_content_tv_finish:
                strContent = etContent.getText().toString().trim();
                if(TextUtils.isEmpty(strContent)){
                    showToast("请输入内容");
                } else {
                    switch (intoFlag){
                        case AccountSetActivity.SET_NICKNAME:
                            dorequestNickName();
                            break;
                        case PrivacySecurityActivity.SET_EMAIL:
                            dorequestEmail();
                            break;
                        case PrivacySecurityActivity.SET_PHONE:
                            dorequestPhone();
                            break;
                    }

                }
                break;
        }
    }

    private void dorequestNickName(){
        showLoading();
        params = new AjaxParams();
        params.put("nickName",strContent+"");
        httpRequest.addHeader("Cookie", "userLoginID=" + userLoginID);
        httpRequest.post(Urls.url_nickname,params,callBack,0);
    }

    private void dorequestEmail(){

    }

    private void dorequestPhone(){

    }

    @Override
    protected void handlerBean(int requestCode, BaseBean baseBean) {
        super.handlerBean(requestCode, baseBean);
        if(baseBean.success){
            String data = String.valueOf(baseBean.data);
            switch (requestCode){
                case 0://修改昵称
                    intent = new Intent();
                    intent.putExtra("setcontent",strContent);
                    setResult(intoFlag,intent);
                    finish();
                    AnimUtil.pushRightInAndOut(this);
                    break;
            }
        } else {
            if(!TextUtils.isEmpty(baseBean.errMsg)){
                showToast(baseBean.errMsg);
            }
        }
    }
}
